package cn.moon.utils;

import java.io.StringWriter;

public class ByteHexUtils {

    public static String toHexString(byte b){

        int val = b & 0xFF;

        int b1 = val / 16;
        int b2 = val % 16;

        char c1 = b1 <= 9 ? (char)(b1 + 48) : (char)(b1 + 55);
        char c2 = b2 <= 9 ? (char)(b2 + 48) : (char)(b2 + 55);

        StringWriter sw = new StringWriter(2);

        sw.write(c1);
        sw.write(c2);

        return sw.toString();

    }

    public static String toHexString(byte[] bytes){

        StringWriter sw = new StringWriter(2);

        for(byte b : bytes){

            int val = b & 0xFF;

            int b1 = val / 16;
            int b2 = val % 16;

            char c1 = b1 <= 9 ? (char)(b1 + 48) : (char)(b1 + 55);
            char c2 = b2 <= 9 ? (char)(b2 + 48) : (char)(b2 + 55);

            sw.write(c1);
            sw.write(c2);

        }

        return sw.toString();

    }

}
